<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Introduce extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('introduce');
        $this->load->library('session');
        $this->output->enable_profiler(TRUE);
        $this->load->model('introduce/introduce_model');
    }

    function manager_introduce() {
        $columns = array(
                array(
                        'name' => 'ID',
                        'type' => 'id',
                        'index' => 'introduce_id',
                ),
                array(
                        'name' => 'Tên',
                        'type' => 'text',
                        'filter' => 'name',
                        'index' => 'name',
                ),
                array(
                        'name' => 'Ngày tạo',
                        'type' => 'date',
                        'index' => 'created',
                ),
                array(
                        'name' => 'Trạng thái',
                        'type' => 'publish',
                        'index' => 'status',
                        'options' => array('0' => 'Tắt', '1' => 'Bật')
                ),
            );
        $this->grid->columns($columns);
        $this->grid->datasource('introduce/introduce_model','get_introduces');
        $this->grid->set_title('introduce Manager');
        $this->grid->add_button('Thêm mới',admin_url('*/introduce/add_introduce'),'add');
        $this->grid->set_row_action('edit_introduce');
        $this->grid->total_rows($this->introduce_model->get_count());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/introduce/delete_introduce'),
                'confirm'  => 'Are you sure?'
        ));
        $this->grid->add_mass_action('status', array(
                'label'=>'Thay đổi trạng thái',
                'url'      => admin_url('*/introduce/change_status_introduce'),
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_introduce() {
        $introduce_id = $this->input->get_param('id', false);
        $this->load->model('introduce/introduce_model');
        if ($introduce_id) {
            $data = $this->introduce_model->get_introduce($introduce_id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
                'id' => 'introduce_form',
                'action' => admin_url('*/introduce/edit_post_introduce' , array('id' => $introduce_id)),
                'enctype' => 'multipart/form-data',
                ));
        $this->form->add_filedset('ganeral', array('legend' => 'General'));
        $this->form->add_filed('name', 'text', array('label' => 'Tiêu đề', 'name' => 'name', 'required' => true,));
        $this->form->add_filed('alias', 'alias', array('label' => $this->__('Alias'), 'name' => 'alias',));
        $this->form->add_filed('image', 'image', array('label' => 'Hình ảnh', 'name' => 'image',));
        $this->form->add_filed('status', 'select', array('label' => 'Trạng thái', 'name' => 'status', 'values' => array('Tắt', 'Bật')));
        $this->form->add_filed('fulltext', 'editor', array('label' => 'Nội dung', 'name' => 'fulltext',));
        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }

    function add_introduce() {
        $this->_forward('edit_introduce');
    }

    function delete_introduce() {
        $cids = explode(',', $this->input->get_param('introduce_id',false));
        $this->load->model('introduce/introduce_model');
        foreach ($cids as $cid) {
            $this->introduce_model->delete_introduce($cid);
        }
        $this->_redirect(admin_url('*/introduce/manager_introduce'));
    }

    function change_status_introduce() {
        $cids = explode(',', $this->input->get_param('introduce_id',false));
        $this->load->model('introduce/introduce_model');
        foreach ($cids as $cid) {
            $this->introduce_model->change_status_introduce($cid);
        }
        $this->_redirect(admin_url('*/introduce/manager_introduce'));
    }

    function edit_post_introduce() {
        $introduce_id = $this->input->get_param('id', false);
        $this->load->model('introduce/introduce_model');
        if (($data = $this->input->post())) {
            $config['upload_path'] = './images/content/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);

            if ($this->upload->do_upload('image')) {
                $data['image'] = 'images/content/' . $this->upload->get_file_name();
            }
            if ($introduce_id) {
                $this->introduce_model->update_introduce($data, $introduce_id);
            } else {
                $this->introduce_model->insert_introduce($data);
            }
            $this->_redirect(admin_url('*/introduce/manager_introduce'));
        }
    }
}
?>